package com.youhome.api.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.CustomCompanyTokenDetails;
import com.youhome.service.model.Principal;
import com.youhome.utils.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chen365 on 2019/03/22.
 */
//@RestController
//@Api(tags = "测试")
//@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation(value = "测试", notes = "测试", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/token")
    public Object token(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        Map<String,Object> maps = new HashMap<>();
        maps.put("b",2);
        maps.put("a",1);
        maps.put("c",3);
        String sign = ValidationUtils.generateSign(maps);

        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://localhost:8081/api/v1/test/checkSign?c=3&a=1&b=2&sign="+sign, String.class);
        return new Result(ResultCode.SUCCESS,forEntity);
    }

    @ApiOperation(value = "测试", notes = "测试", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/checkSign")
    public Object checkSign(HttpServletRequest request){
        System.err.println("ValidationUtils:"+ValidationUtils.validateSign(request));
        List<String> keys = new ArrayList<>(request.getParameterMap().keySet());
        for(String key : keys){
            System.err.println(key);
        }
        keys.remove("sign");//排除sign参数
        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            sb.append(key).append("=").append(request.getParameter(key)).append("&");//拼接字符串
        }
        String linkString = sb.toString();
        linkString = StringUtils.substring(linkString, 0, linkString.length() - 1);//去除最后一个'&'
        System.err.println("link:" + linkString);
        String password = "c303701a92932475b516e5033b7a3e0b";
        DigestUtils.md5Hex(linkString + password);
        return new Result(ResultCode.SUCCESS,linkString);
    }


    @ApiOperation(value = "测试2", notes = "测试2", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/check")
    public Object check(@ApiIgnore @AuthenticationPrincipal CustomCompanyTokenDetails principal) throws CustomException {
        System.err.println("principal==>>"+principal.toString());
        return new Result(ResultCode.SUCCESS,principal);
    }
}
